package com.yxq.mdt.app.modules.socialcontact.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.widget.ImageView;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.wuhenzhizao.titlebar.widget.CommonTitleBar;
import com.yxq.mdt.app.R;
import com.yxq.mdt.app.base.BaseActivity;
import com.yxq.mdt.app.modules.bottomsheet.PublishBottomSheetFragment;
import com.yxq.mdt.app.modules.socialcontact.adapter.MainSocialCircleMoreAdapter;
import com.yxq.mdt.app.modules.socialcontact.adapter.MainSocialCircleMoreMyAdapter;
import com.yxq.mdt.app.modules.socialcontact.bean.ByPageListBean;
import com.yxq.mdt.app.modules.socialcontact.bean.MainSocialCircleBean;
import com.yxq.mdt.app.modules.socialcontact.model.MainSocialContactModel;
import com.yxq.mdt.app.modules.socialcontact.presenter.MainSocialMyCircleContract;
import com.yxq.mdt.app.modules.socialcontact.presenter.MainSocialMyCirclePresenter;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 我的全部圈子列表
 */
public class CircleRecommendMyListAc extends BaseActivity<MainSocialMyCirclePresenter> implements MainSocialMyCircleContract.View {

    @BindView(R.id.titlebar)
    CommonTitleBar titlebar;
    @BindView(R.id.circle_recommend_recycler)
    RecyclerView circleRecommendRecycler;
    @BindView(R.id.magic_publish)
    ImageView magicPublish;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;
    MainSocialCircleMoreMyAdapter socialCircleMoreAdapter;
    PublishBottomSheetFragment publishBottomSheetFragment;
    String topic, isPublish, isPublishVoice, isPublishImage;

    @Override
    public int getLayoutId() {
        return R.layout.circle_recommend_list_activity;
    }

    @Override
    public void initView() {
        isPublish = getIntent().getStringExtra("publish");
        isPublishVoice = getIntent().getStringExtra("publishVoice");
        isPublishImage = getIntent().getStringExtra("publishImage");
        circleRecommendRecycler.setLayoutManager(new LinearLayoutManager(mActivity));
        socialCircleMoreAdapter = new MainSocialCircleMoreMyAdapter(R.layout.circle_list_item_my, null);
        circleRecommendRecycler.setAdapter(socialCircleMoreAdapter);
        socialCircleMoreAdapter.setOnItemClickListener((adapter, view1, position) -> {
            if (TextUtils.isEmpty(isPublish) && TextUtils.isEmpty(isPublishVoice) && TextUtils.isEmpty(isPublishImage)) {
                Intent intent = new Intent(mActivity, CircleTheHomepageAc.class);
                intent.putExtra("CircleName", socialCircleMoreAdapter.getItem(position).getGroupname());
                intent.putExtra("id", socialCircleMoreAdapter.getItem(position).getId());
                startActivity(intent);
            } else {
                Intent intent = new Intent();
                intent.putExtra("CircleName", socialCircleMoreAdapter.getItem(position).getGroupname());
                intent.putExtra("id", socialCircleMoreAdapter.getItem(position).getId());
                setResult(RESULT_OK, intent);
                finish();
            }
        });
        refreshLayout.setOnRefreshListener(refreshLayout -> {
            page = 1;
            mPresenter.getMyGroupList(page, 30);
        });
        refreshLayout.setOnLoadMoreListener(refreshLayout -> {
            page++;
            mPresenter.getMyGroupList(page, 30);
        });
    }

    @Override
    public void initData(@Nullable Bundle savedInstanceState) {
        mPresenter = new MainSocialMyCirclePresenter(this, new MainSocialContactModel());
        refreshLayout.autoRefresh();
    }

    @Override
    public void onLoadRecommendSuccess(MainSocialCircleBean mainSocialCircleBean) {
        refreshLayout.finishRefresh();
        refreshLayout.finishLoadMore();
    }

    @Override
    public void onLoadRecommendFailed() {
        refreshLayout.finishRefresh();
        refreshLayout.finishLoadMore();

    }

    @Override
    public void onLoadMyGroupListFailed() {
        refreshLayout.finishRefresh();
        refreshLayout.finishLoadMore();
    }

    @Override
    public void onLoadMyGroupListSuccess(MainSocialCircleBean mainSocialCircleBean) {
        refreshLayout.finishRefresh();
        refreshLayout.finishLoadMore();
        if (page == 1) {
            socialCircleMoreAdapter.setNewData(mainSocialCircleBean.getList());
        } else {
            if (mainSocialCircleBean.getList() != null && mainSocialCircleBean.getList().size() != 0) {
                socialCircleMoreAdapter.addData(mainSocialCircleBean.getList());
            } else {
                refreshLayout.finishLoadMoreWithNoMoreData();
            }
        }
    }

    @Override
    public void onLoadByPageListSuccess(ByPageListBean byPageListBean) {
        refreshLayout.finishRefresh();
        refreshLayout.finishLoadMore();

    }

    @Override
    public void onLoadByPageListFailed() {

    }


    @OnClick(R.id.magic_publish)
    public void onViewClicked() {
        publishBottomSheetFragment = new PublishBottomSheetFragment(titlebar.getCenterTextView().getText().toString(), "");
        publishBottomSheetFragment.show(getSupportFragmentManager(), "publishBottomSheetFragment");
    }

}
